﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using VMDS.Business.Entities;

namespace VMDS.Data
{
    public class SqlDataGateway:IEntityCrud
    {
        private ModelContainer _db = new ModelContainer();

        #region Implementation of IEntityCrud

        public List<VM> GetVMs()
        {
            return new List<VM>(_db.VMs);
        }

       

        public int AddVM(VM vm)
        {
            var newvm=_db.VMs.CreateObject();
            newvm.Name = vm.Name;
            newvm.OSId = vm.OSId;
            newvm.SubjectId = vm.SubjectId;
            newvm.Description = vm.Description;
            newvm.CreatedBy = Thread.CurrentPrincipal.Identity.Name;
            newvm.Date = DateTime.Now;
            newvm.Version = vm.Version;
            _db.VMs.AddObject(newvm);
            _db.SaveChanges();
            return newvm.Id;
        }

        public void UpdateVM(VM vm)
        {
            VM old = GetVM(vm.Id);
            old.Name = vm.Name;
            old.OSId = vm.OSId;
            old.Description = vm.Description;
            old.Date = DateTime.Now;
           // old.CreatedBy = vm.CreatedBy;
            old.SubjectId = vm.SubjectId;
            old.Version = vm.Version;
            old.Visible = vm.Visible;
            _db.SaveChanges();
        }

        public VM GetVM(int id)
        {
            return _db.VMs.SingleOrDefault(m => m.Id == id);
        }
        public VM GetVM(string Name)
        {
            return _db.VMs.SingleOrDefault(m => m.Name.ToLower()==Name.ToLower());
        }

        public void DeleteVM(VM vm)
        {
            _db.VMs.DeleteObject(GetVM(vm.Id));
            _db.SaveChanges();
        }

        public List<OS> GetOSs()
        {
            return new List<OS>(_db.OSs);
        }

        public void AddOS(OS os)
        {
            _db.OSs.AddObject(os);
            _db.SaveChanges();
        }

        public void UpdateOS(OS os)
        {
            OS old = GetOS(os.Id);
            old.Name = os.Name;
            old.Version = os.Version;
            _db.SaveChanges();
        }

        public OS GetOS(int id)
        {
            return _db.OSs.SingleOrDefault(m => m.Id == id);
        }

        public void DeleteOS(OS os)
        {
            _db.OSs.DeleteObject(GetOS(os.Id));
            _db.SaveChanges();
        }

        public List<ScreenShot> GetScreenShots()
        {
            return new List<ScreenShot>(_db.ScreenShots);
        }

        public void AddScreenShot(ScreenShot scr)
        {
            _db.ScreenShots.AddObject(scr);
            _db.SaveChanges();
        }

        public void UpdateScreenShot(ScreenShot scr)
        {
            ScreenShot old = GetScreenShot(scr.Id);
            old.FileName = scr.FileName;
            old.VMId = scr.VMId;
            _db.SaveChanges();
        }

        public ScreenShot GetScreenShot(int id)
        {
            return _db.ScreenShots.SingleOrDefault(s => s.Id == id);
        }

        public void DeleteScreenShot(ScreenShot scr)
        {
            _db.ScreenShots.DeleteObject(GetScreenShot(scr.Id));
            _db.SaveChanges();
        }

        public List<Subject> GetSubjects()
        {
            return new List<Subject>(_db.Subjects);
        }

        public void AddSubject(Subject subj)
        {
            _db.Subjects.AddObject(subj);
            _db.SaveChanges();
        }

        public void UpdateSubject(Subject subj)
        {
            Subject old = GetSubject(subj.Id);
            old.Name = subj.Name;
            _db.SaveChanges();
        }

        public Subject GetSubject(int id)
        {
            return _db.Subjects.SingleOrDefault(s => s.Id == id);
        }

        public void DeleteSubject(Subject subj)
        {
            _db.Subjects.DeleteObject(GetSubject(subj.Id));
            _db.SaveChanges();
        }

        public List<ClientPC> GetClientPCs()
        {
            return new List<ClientPC>(_db.ClientPCs);
        }

        public void AddClientPC(ClientPC pc)
        {
            var newpc = _db.ClientPCs.CreateObject();
            newpc.Name = pc.Name;
            string ip=string.Empty;
            if (pc.IP!=null) ip= pc.IP;
            newpc.IP = ip;
            _db.ClientPCs.AddObject(newpc);
            _db.SaveChanges();
        }

        public void UpdateClientPC(ClientPC pc)
        {
            ClientPC old = GetClientPC(pc.Id);
            old.IP = pc.IP;
            old.Name = pc.Name;
            _db.SaveChanges();

        }

        public ClientPC GetClientPC(int id)
        {
            return _db.ClientPCs.SingleOrDefault(s => s.Id == id);
        }

        public void DeleteClientPC(ClientPC pc)
        {
            _db.ClientPCs.DeleteObject(GetClientPC(pc.Id));
            _db.SaveChanges();
        }

        public List<Task> GetTasks()
        {
            return new List<Task>(_db.Tasks);
        }

        public void AddTask(Task task)
        {
            _db.Tasks.AddObject(task);
            _db.SaveChanges();
        }

        public void UpdateTask(Task task)
        {
            Task old = GetTask(task.Id);
            old.Priority = task.Priority;
            old.State = task.State;
            old.VMId = task.VMId;
            old.CreatedBy = task.CreatedBy;
            old.Completed = task.Completed;
            old.ClientPCId = task.ClientPCId;
            old.IsManual = task.IsManual;
            _db.SaveChanges();
        }

        public Task GetTask(int id)
        {
            return _db.Tasks.SingleOrDefault(t => t.Id == id);
        }

        public void DeleteTask(Task task)
        {
            _db.Tasks.DeleteObject(GetTask(task.Id));
            _db.SaveChanges();
        }

        #endregion
    }
}
